package com.shushan.mixedexam.bean;

import android.text.TextUtils;

import com.android.net.bean.NetBaseBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: ExerciseBean
 * @Desciption: 习题bean
 * @author: yichaohua
 * @date: 2018-04-18
 */
public class ExerciseBean extends NetBaseBean {

    public int id;//试题ID
    public String exercises_type;//试题类型SINGLE单选MULTI-多选
    public String exercises_level;
    public String exercises_content;//习题类容
    public String exercises_imgs;//图片jsonarray
    public String content_type;//内容类型 TEXT 纯文本，TEXT_IMG 图文
    public String outline_detail_ids;//关联知识点
    public String subject_id;//学科ID
    public String image_size;
    public int outline_detail_id;//根据该知识点查询出
    public int outline_id;
    public int child_collect;//0未收藏1已收藏
    public int my_answer_id;//上次选择的答案ID
    public int score;

    private List<ExerciseOptionBean> optionList = new ArrayList<ExerciseOptionBean>();
    private List<ExerciseVideoBean> videoList = new ArrayList<ExerciseVideoBean>();


    @Override
    public void initByJson(JSONObject jsonObject) throws JSONException {
        id = jsonObject.optInt("id",0);
        exercises_type = jsonObject.optString("exercises_type","");
        exercises_level = jsonObject.optString("exercises_level","");
        exercises_content = jsonObject.optString("exercises_content","");
        if(!TextUtils.isEmpty(exercises_content)){
            JSONArray jsonArray = new JSONArray(exercises_content);
            exercises_content = "";
            for (int i = 0;i < jsonArray.length();i++){
                exercises_content += jsonArray.optString(i);
            }
        }

        content_type = jsonObject.optString("content_type","");
        outline_detail_ids = jsonObject.optString("outline_detail_ids","");
        subject_id = jsonObject.optString("subject_id","");

        int imgWidth = 400;
        image_size = jsonObject.optString("image_size","");
        if(!TextUtils.isEmpty(image_size)){
            JSONArray jsonArray = new JSONArray(image_size);
            for (int i = 0;i < jsonArray.length();i++){
                JSONObject json = jsonArray.optJSONObject(i);
                int width = json.optInt("generateWidth");
                if(imgWidth > width){
                    imgWidth = width;
                }
            }
        }

        exercises_imgs = jsonObject.optString("exercises_imgs","");
        if(!TextUtils.isEmpty(exercises_imgs)){
            exercises_imgs = exercises_imgs.replace("{width}",imgWidth + "");
        }

        outline_detail_id = jsonObject.optInt("outline_detail_id",0);
        outline_id = jsonObject.optInt("outline_id",0);
        child_collect = jsonObject.optInt("child_collect",0);
        my_answer_id = jsonObject.optInt("my_answer_id",0);
        score = jsonObject.optInt("score",0);

        JSONArray optionArray = jsonObject.optJSONArray("exercise_option");
        if(optionArray != null){
            for (int i = 0;i < optionArray.length();i++){
                JSONObject json = optionArray.optJSONObject(i);
                ExerciseOptionBean exerciseOptionBean = new ExerciseOptionBean();
                exerciseOptionBean.initByJson(json);
                optionList.add(exerciseOptionBean);
            }

        }

        JSONArray videoArray = jsonObject.optJSONArray("video");
        if(videoArray != null){
            for (int i = 0;i < videoArray.length();i++){
                JSONObject json = videoArray.optJSONObject(i);
                ExerciseVideoBean exerciseVideoBean = new ExerciseVideoBean();
                exerciseVideoBean.initByJson(json);
                videoList.add(exerciseVideoBean);
            }

        }

    }

    public List<ExerciseOptionBean> getOptionList() {
        return optionList;
    }

    public void setOptionList(List<ExerciseOptionBean> optionList) {
        this.optionList = optionList;
    }

    public List<ExerciseVideoBean> getVideoList() {
        return videoList;
    }

    public void setVideoList(List<ExerciseVideoBean> videoList) {
        this.videoList = videoList;
    }
}
